import pygtk
pygtk.require('2.0')
import gtk
import os
import freemode
import lessonMode
import lessonchooser
import assessmentChooser
import pygame

IMAGE_SOURCE = lessonchooser.TYPING_ROOT + os.sep + "images" + os.sep

class TypingGame:

    def __init__(self, win):
        # Create a new window
        pygame.init()
        self.window = win
        #self.window.set_size_request(800, 600)

        # Set the window title
        #self.window.set_title("Mode Chooser")

        # Set a handler for delete_event that immediately
        # exits GTK.
        self.quitter = self.window.connect("delete_event", self.delete_event)

        # Sets the border width of the window.
        self.window.set_border_width(20)

        #Create a Vbox for all elements of the program, to show/hide them
        self.vbox = gtk.VBox(False, 0)
        
        # Create a Vbox for the buttons
        self.modebox = gtk.VBox(False, 0)
        
        # Put the box in the main window
        self.window.add(self.vbox)
        self.vbox.show()
        self.vbox.add(self.modebox)
        self.modebox.show()

        # Create first button
        image = gtk.Image()
        pixbuf = gtk.gdk.pixbuf_new_from_file(IMAGE_SOURCE + os.sep + "free.png").scale_simple(150, 150, gtk.gdk.INTERP_BILINEAR)
        image.set_from_pixbuf(pixbuf)
        button = gtk.Button()
        button.set_image(image)
        # When the button is clicked, we call the "callback" method
        # with a pointer to "Sound Game" as its argument
        button.connect("clicked", self.callback, "Free Mode")

        # Insert button 1 into the box
        self.modebox.add(button)
        button.show()
        
        # Create second button
        image = gtk.Image()
        pixbuf = gtk.gdk.pixbuf_new_from_file(IMAGE_SOURCE + os.sep + "lesson.png").scale_simple(150, 150, gtk.gdk.INTERP_BILINEAR)
        image.set_from_pixbuf(pixbuf)
        button = gtk.Button()
        button.set_image(image)

        # When the button is clicked, we call the "callback" method
        # with a pointer to "Lesson Mode" as its argument
        button.connect("clicked", self.callback, "Lesson Mode")

        # Insert button 2 into box
        self.modebox.add(button)
        button.show()
               
        #Create the third button.  Will use text for now, should eventually get an image
        button = gtk.Button()
        image = gtk.Image()
        pixbuf = gtk.gdk.pixbuf_new_from_file(IMAGE_SOURCE + os.sep + "assessment.gif").scale_simple(150, 150, gtk.gdk.INTERP_BILINEAR)
        image.set_from_pixbuf(pixbuf)
        button.set_image(image)
        button.connect("clicked", self.callback, "Assessment Mode")
        
        # Insert button 3 into box
        self.modebox.add(button)
        button.show()
        
        #display the window
        self.window.show()

    # Activates one of the game modes.  Runs the mode listed
    # on the button, which is also the data we are passed when it
    # is pressed.
    def callback(self, widget, data=None):
        #Hide the mode choices
        self.modebox.hide()
        
        if(data == "Free Mode"):
            #Put a Free Mode Object in the window
            FM = freemode.FreeMode(self.window)
            self.vbox.add(FM)
            FM.set_flags(gtk.CAN_DEFAULT)
            FM.grab_default()
            FM.show()
            
            #Block the delete handler here, so we don't call it twice when we quit 
            self.window.handler_block(self.quitter)

            #Run Free Mode.  Will return True if it ended because the user closed the window            
            quit = FM.runGame()

            #If quit was true, the user closed the window,
            #so call the event we would have called from this level
            #so that we call gtk.main_quit correctly.
            if(quit):
                self.delete_event(False, False, False)
                return

            #Unblock, so the user can now close the window again            
            self.window.handler_unblock(self.quitter)

            #Clear away freemode, so we can set it up again correctly next time            
            FM.hide()
            del FM
        elif(data == "Lesson Mode"):
            #Create a Lesson Mode object in the window
            self.scrolledLM = gtk.ScrolledWindow()
            self.LM = lessonchooser.lessonChooser(self.window, self.vbox, self.scrolledLM)
            
            self.scrolledLM.add_with_viewport(self.LM)
            self.scrolledLM.set_policy(gtk.POLICY_AUTOMATIC,gtk.POLICY_AUTOMATIC)
            #self.scrolledLM.add(self.LM)
            self.vbox.add(self.scrolledLM)
            self.LM.show()
            self.scrolledLM.show()
            #Block the delete handler here, so we don't call it twice when we quit             
            self.window.handler_block(self.quitter)

            #Run Free Mode.  Will return True if it ended because the user closed the window                        
            quit = self.LM.run()

            #If quit was true, the user closed the window,
            #so call the event we would have called from this level
            #so that we call gtk.main_quit correctly.            
            if(quit):
                self.delete_event(False, False, False)
                return

            #Unblock, so the user can now close the window again                        
            self.window.handler_unblock(self.quitter)

            self.scrolledLM.hide()

            #Clear away freemode, so we can set it up again correctly next time            
            del self.LM
            
        elif(data == "Assessment Mode"):
            self.AM = assessmentChooser.assessmentChooser(self.window)
            self.vbox.add(self.AM)
            self.AM.show()

            #Block the delete handler here, so we don't call it twice when we quit 
            self.window.handler_block(self.quitter)

            #Run Free Mode.  Will return True if it ended because the user closed the window            
            quit = self.AM.run()

            #If quit was true, the user closed the window,
            #so call the event we would have called from this level
            #so that we call gtk.main_quit correctly.
            if(quit):
                self.delete_event(False, False, False)
                return

            #Unblock, so the user can now close the window again            
            self.window.handler_unblock(self.quitter)

            #Clear away freemode, so we can set it up again correctly next time            
            del self.AM

        #Restore the mode choices
        self.modebox.show()

    # Quits the program, after gtk.main() has started
    def delete_event(self, widget, event, data=None):
        gtk.main_quit()
        return False

   
#Start running gtk, and wait until it is quit to end.
def main():
    mainwin = gtk.Window(gtk.WINDOW_TOPLEVEL)
    mainwin.set_title('Typing Program')
    display = mainwin.get_display()
    screen = gtk.gdk.Display.get_default_screen(display)
    mainwin.set_geometry_hints( None, screen.get_width(), screen.get_height(), screen.get_width(), screen.get_height(), screen.get_width(), screen.get_height() )
    mainwin.set_resizable(False)
    t = TypingGame(mainwin)
    gtk.main()
    return 0       

if __name__ == "__main__":
    main()